package com.aaa.service.back.Impl;

import com.aaa.entity.PagerData;
import com.aaa.entity.Warehouse;
import com.aaa.entity.WarehouseDetails;
import com.aaa.entity.WarehouseDetailsQuery;
import com.aaa.mapper.back.WarehouseMapper;
import com.aaa.service.back.WarehouseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WarehouseServiceImpl implements WarehouseService {

    @Resource
    private WarehouseMapper warehouseMapper;

    @Override
    public List<Map<String, Object>> findWarehouse() {
        return warehouseMapper.findWarehouse();
    }

    @Override
    public Map<String, Object> findWarehouseByWarehouseId(Integer warehouseId) {
        return warehouseMapper.findWarehouseByWarehouseId(warehouseId);
    }

    @Override
    public PagerData<WarehouseDetails> listByPage(WarehouseDetailsQuery warehouseDetailsQuery, Integer curPage, Integer pageSize) {
        //当前页记录的起始位置
        int start = (curPage-1)*pageSize;
        //构建条件
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("wdq", warehouseDetailsQuery);
        map.put("start", start);
        map.put("pageSize",pageSize);
        //获取当前页的数据
        List<WarehouseDetails> warehouseDetailsList = warehouseMapper.findByPage(map);
        //获取当前条件的总记录数
        int totalCount = warehouseMapper.findTotalCount(map);
        //获取总页数
        int totalPage = totalCount%pageSize==0?
                totalCount/pageSize:
                totalCount/pageSize+1;

        //封装PagerData
        PagerData<WarehouseDetails> pd = new PagerData<WarehouseDetails>();
        pd.setCurPage(curPage);
        pd.setPageSize(pageSize);
        pd.setTotalCount(totalCount);
        pd.setTotalPage(totalPage);
        pd.setDataList(warehouseDetailsList);

        return pd;
    }

    @Override
    public int addWarehouse(Warehouse warehouse) {
        return warehouseMapper.addWarehouse(warehouse);
    }

    @Override
    public int editWarehouse(Warehouse warehouse) {
        return warehouseMapper.editWarehouse(warehouse);
    }

    @Override
    public int addWarehouseDetails(WarehouseDetails warehouseDetails) {
        return warehouseMapper.addWarehouseDetails(warehouseDetails);
    }

    @Override
    public int editWarehouseDetails(Integer goodsCount, String goodsId, Integer warehouseId) {
        Map map = new HashMap<String,Object>();
        map.put("goodsCount",goodsCount);
        map.put("goodsId",goodsId);
        map.put("warehouseId",warehouseId);
        return warehouseMapper.editWarehouseDetails(map);
    }

    @Override
    public WarehouseDetails FindWarehouseDetailsByGoodsIdAndWarehouseId(String goodsId, Integer warehouseId) {
        Map map = new HashMap<String,Object>();
        map.put("goodsId",goodsId);
        map.put("warehouseId",warehouseId);
        return warehouseMapper.FindWarehouseDetailsByGoodsIdAndWarehouseId(map);
    }
}
